//接收消息
chrome.runtime.onMessage.addListener(  function(request, sender, sendResponse) { 
	if(request.cmd== "mycmd") {
		setCookie("username",request.data1);
		setCookie("password",request.data2);
	}
	if(request.cmd== "prohibit"){
		sendResponse({"uid":getCookie(oc),"username":getCookie("username"),"password":getCookie("password"),"account":getCookie(oc+"_account")}); 
		ajax();
	}
	if(request.cmd== "all-prohibit"){
		sendResponse({"uid":getCookie(oc),"username":getCookie("username"),"password":getCookie("password")}); 
	}
	if(request.cmd== "ajax"){
		sendResponse(_ajax(request));
	}
});

var oc = "grapp";//only code
var url = "http://www.qianfuzhang.com/chrome";
var _loginCookie;
function delCookie(name) {
  chrome.cookies.remove({"name": name,"url":url});
}
function setCookie(name,value) {
  chrome.cookies.set({"name": name,"value":value,"url":url},function(cookie){
	  _loginCookie = cookie.value;
	  $.cookie(name,_loginCookie);
  });
}
function getCookie(name) {
  chrome.cookies.get({"name": name,"url":url},function(cookie){
	  _loginCookie = cookie.value;
	  $.cookie(name,_loginCookie);
  });	
  return $.cookie(name);
}

/*
*	在BackGround中处理异步避免(https)
*   by 2015/07/16
*/
function _ajax(obj){
	//http://grapp.qianfuzhang.com/index/gettaoke/?list_time=1&itemid=520300335183
	
	if(typeof(obj.dataType) == "undefined"){
		obj.dataType = "html";	
	}
	
	if(obj.async !== true){
		obj.async = false;	
	}

	var _data;
	$.ajax({  
		url: obj.url,
		dataType:obj.dataType,
		async:obj.async,
		success: function(data){
			_data = data;
		}
	});
	return _data;
}




jQuery.cookie = function(name, value, options) { 
    if (typeof value != 'undefined') { // name and value given, set cookie 
        options = options || {}; 
        if (value === null) { 
            value = ''; 
            options.expires = -1; 
        } 
        var expires = ''; 
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { 
            var date; 
            if (typeof options.expires == 'number') { 
                date = new Date(); 
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); 
            } else { 
                date = options.expires; 
            } 
            expires = '; expires=' + date.toUTCString(); 
        } 
        var path = options.path ? '; path=' + (options.path) : ''; 
        var domain = options.domain ? '; domain=' + (options.domain) : ''; 
        var secure = options.secure ? '; secure' : ''; 
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); 
    } else { 
        var cookieValue = null; 
        if (document.cookie && document.cookie != '') { 
            var cookies = document.cookie.split(';'); 
            for (var i = 0; i < cookies.length; i++) { 
                var cookie = jQuery.trim(cookies[i]); 
                if (cookie.substring(0, name.length + 1) == (name + '=')) { 
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); 
                    break; 
                } 
            } 
        } 
        return cookieValue; 
    } 
};